package com.sunny.ynhz.ui.message;

import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.sunny.ynhz.R;
import com.sunny.ynhz.api.NetUrl;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;
import com.sunny.ynhz.ui.view.MyRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class ViewGroupUserActivity extends AppBaseTranslationTitleBarActivity {

    private String gid = "";
    private RequestQueue mQueue;
    private int page =1;
    private BaseAdapter mAdapter;
    private MyRefreshLayout myRefreshLayout;
    private ArrayList<HashMap<String,String>> datalist;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_group_user);
        setTitle("群成员");
        gid = getIntent().getStringExtra("gid");
        mQueue = Volley.newRequestQueue(this);
        datalist = new ArrayList<>();
        initView();
        loadData();

    }

    private void loadData() {
         String url = NetUrl.baseUrl+"rong/web/getUserByGroupId?groupId="+gid+"&page="+page+"&pageSize=10";
       Log.i("TAG",url);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = jsonObject.getInt("status");
                    if(status == 200){
                        JSONArray  data = jsonObject.getJSONArray("result");
                        Log.i("TAG","用户数量"+data.length());
                        for(int i=0;i<data.length();i++){
                            HashMap<String,String> hashMap = new HashMap<>();
                            JSONObject info = data.getJSONObject(i);
                            hashMap.put("id",info.getString("id"));
                            hashMap.put("name",info.getString("name"));
                            hashMap.put("phone",info.getString("mobile"));
                            datalist.add(hashMap);
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if(page == 1){
                    myRefreshLayout.setRefreshing(false);
                }else{
                    myRefreshLayout.setLoading(false);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        mQueue.add(stringRequest);
    }

    private void initView() {
        myRefreshLayout = (MyRefreshLayout)findViewById(R.id.refresh);
        myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                myRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        datalist.clear();
                        loadData();
                    }
                },1500);
            }
        });
        myRefreshLayout.setOnLoadListener(new MyRefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                myRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page ++;
                        loadData();
                    }
                },1500);
            }
        });
        listView = (ListView)findViewById(R.id.listView);

        datalist = new ArrayList<>();
        mAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return datalist.size();
            }

            @Override
            public Object getItem(int i) {
                return i;
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View convertView, ViewGroup viewGroup) {
                if(convertView == null){
                    convertView = LayoutInflater.from(ViewGroupUserActivity.this).inflate(R.layout.item_contacts,viewGroup,false);
                }
                TextView title = (TextView)convertView.findViewById(R.id.id_name);
                title.setText(datalist.get(i).get("name"));

                TextView number = (TextView)convertView.findViewById(R.id.id_number);
                number.setText(datalist.get(i).get("phone"));
                return  convertView;
            }
        };
        listView.setAdapter(mAdapter);

    }

}
